package com.gzdh.dishrecognition.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.entity.BmcmEntity;
import com.gzdh.dishrecognition.entity.MacMainpSbruanDinnerEntity;
import com.gzdh.dishrecognition.ui.widget.SwipeLayout;

import java.util.ArrayList;
import java.util.List;

public class BmcmInfoAdapter extends RecyclerView.Adapter<BmcmInfoAdapter.ViewHolder> {

    private Context mcontext;
    private TextView idTv;

    private TextView nameTv;
    private TextView startTimeTv;
    private TextView endTimeTv;
    private TextView deleteBut;
    private TextView editBut;

    private View getViews() {
        LinearLayout mainLinear = new LinearLayout(mcontext);
        mainLinear.setOrientation(LinearLayout.VERTICAL);
        mainLinear.setId(View.generateViewId());
        mainLinear.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        SwipeLayout swipeLayout = new SwipeLayout(mcontext);
        swipeLayout.setId(View.generateViewId());
        swipeLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mainLinear.addView(swipeLayout);

        LinearLayout itemLinear = new LinearLayout(mcontext);
        itemLinear.setId(View.generateViewId());
        itemLinear.setOrientation(LinearLayout.HORIZONTAL);
        itemLinear.setPadding(0, 10, 0, 10);
        itemLinear.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        swipeLayout.addView(itemLinear);

        idTv = new TextView(mcontext);
        idTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        idTv.setGravity(Gravity.CENTER);
        idTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        itemLinear.addView(idTv);

        nameTv = new TextView(mcontext);
        nameTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        nameTv.setGravity(Gravity.CENTER);
        nameTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        itemLinear.addView(nameTv);

        startTimeTv = new TextView(mcontext);
        startTimeTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        startTimeTv.setGravity(Gravity.CENTER);
        startTimeTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        itemLinear.addView(startTimeTv);

        endTimeTv = new TextView(mcontext);
        endTimeTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        endTimeTv.setGravity(Gravity.CENTER);
        endTimeTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        itemLinear.addView(endTimeTv);

        LinearLayout switchLayout = new LinearLayout(mcontext);
        switchLayout.setId(View.generateViewId());
        switchLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
        switchLayout.setOrientation(LinearLayout.HORIZONTAL);
        swipeLayout.addView(switchLayout);

        editBut = new TextView(mcontext);
        editBut.setText("修改");
        editBut.setLayoutParams(new LinearLayout.LayoutParams(100, ViewGroup.LayoutParams.MATCH_PARENT));
        editBut.setBackgroundColor(Color.GREEN);
        editBut.setTextColor(Color.BLACK);
        editBut.setGravity(Gravity.CENTER);
        editBut.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        switchLayout.addView(editBut);

        deleteBut = new TextView(mcontext);
        deleteBut.setText("删除");
        deleteBut.setLayoutParams(new LinearLayout.LayoutParams(100, ViewGroup.LayoutParams.MATCH_PARENT));
        deleteBut.setBackgroundColor(Color.RED);
        deleteBut.setTextColor(Color.YELLOW);
        deleteBut.setGravity(Gravity.CENTER);
        deleteBut.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        switchLayout.addView(deleteBut);

        View view = new View(mcontext);
        view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
        view.setBackgroundColor(Color.rgb(204, 204, 204));
        mainLinear.addView(view);
        return mainLinear;
    }


    private List<MacMainpSbruanDinnerEntity> list;

    public BmcmInfoAdapter(Context context) {
        mcontext = context;
        this.list = new ArrayList<>();
    }


    public void setList(List<MacMainpSbruanDinnerEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
         View view = getViews();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        idTv.setText(list.get(position).getDinner().trim());
        nameTv.setText(list.get(position).getDinnerName().trim());
        startTimeTv.setText(list.get(position).getStartTime());
        endTimeTv.setText(list.get(position).getEndTime());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewHolder(View item) {
            super(item);
        }
    }


}
